<?php

// You will need to change the line below to add the directory of the OpenID 
// libraries -- use the absolute path
// set_include_path(get_include_path() . PATH_SEPARATOR . "/path/on/your/server/to/OpenID_Libraries");

set_include_path(get_include_path() . PATH_SEPARATOR . "/usr/local/lib/php/OpenID_Libraries");

require_once(dirname(dirname(dirname(__FILE__))).'/config.php');

really_require_once('Auth/OpenID/Consumer.php');
really_require_once('Auth/OpenID/MySQLStore.php');
really_require_once('Auth/OpenID/DatabaseConnection.php');

really_require_once('Client/Trust.php');
really_require_once('Client/System.php');
really_require_once('Client/Confirmation.php');
really_require_once('Client/Profile.php');
really_require_once('Client/Form.php');
really_require_once('Client/Client.php');

require_once('Store_Moodle.php');
require_once('System_Moodle.php');
require_once('Configuration_Moodle.php');

$openid_client_store = new Client_OpenID_MySQLStore_Moodle();

$openid_client_system = new Client_OpenID_System_Moodle($openid_client_store);

$openid_client_system->log_location('setting up Moodle client');

$openid_client_trust = new Client_OpenID_Trust();

$openid_client_profile = new Client_OpenID_Profile($openid_client_system);

$openid_client_configuration = new Client_OpenID_Configuration_Moodle($openid_client_trust,$openid_client_profile);

$openid_client = new Client_OpenID($openid_client_configuration);


/**
 * This function replaces require_once and attempts to fix a PHP bug
 * that interferes with require_once behavior when it is used inside a
 * function.  require_once, when used inside a function, only puts
 * functions and classes into the global namespace; global variables
 * in the required file are put into the local scope of the function
 * call, which is not what is desired.  Because this module is
 * required in the scope of another function, the bug occurs and we
 * have to put things into the global namespace manually.  Whee!
 */
 
function really_require_once($path) {
    require_once($path);
    foreach (get_defined_vars() as $k => $v) {
        $GLOBALS[$k] = $v;
    }
}

if (!function_exists('fnmatch')) {
function fnmatch($pattern, $string) {
   for ($op = 0, $npattern = '', $n = 0, $l = strlen($pattern); $n < $l; $n++) {
       switch ($c = $pattern[$n]) {
           case '\\':
               $npattern .= '\\' . @$pattern[++$n];
           break;
           case '.': case '+': case '^': case '$': case '(': case ')': case '{': case '}': case '=': case '!': case '<': case '>': case '|':
               $npattern .= '\\' . $c;
           break;
           case '?': case '*':
               $npattern .= '.' . $c;
           break;
           case '[': case ']': default:
               $npattern .= $c;
               if ($c == '[') {
                   $op++;
               } else if ($c == ']') {
                   if ($op == 0) return false;
                   $op--;
               }
           break;
       }
   }

   if ($op != 0) return false;

   return preg_match('/' . $npattern . '/i', $string);
}
}

?>
